import numpy as np
import matplotlib.pyplot as plt
from wordcloud import WordCloud
from pyecharts import options as opts
from pyecharts.charts import Polar, Page
from pyecharts.charts import Pie
import csv
# 折线图

x = [5,7,11,17,19,25]#点的横坐标
k1 = [0.82,0.91,1.00,1.36,1.07,0.65]#线1的纵坐标
k2 = [0.89,0.93,1.14,1.57,1.63,1.93]#线2的纵坐标
plt.plot(x,k1,'s-',color = 'r',label="境内")#s-:方形
plt.plot(x,k2,'o-',color = 'g',label="境外")#o-:圆形
plt.xlabel("天数")
plt.ylabel("新增增幅")
plt.legend(loc = "best")#图例
plt.rcParams['font.sans-serif']=['SimHei'] # 中文
plt.show()


#柱状图
A1 = [6,5,4]

x = np.arange(3) #总共有几组，就设置成几，我们这里有三组，所以设置为3
total_width, n = 0.4, 1   # n有多少个类型
width = total_width / n
x = x - (total_width - width) / 2
plt.bar(x, A1, color = "r",width=width,label='新增例 ')


plt.xlabel("一线城市")
plt.ylabel("新增确诊")
plt.legend(loc = "best")
plt.xticks([0,1,2],['北京','上海','广州'])
plt.ylim((0, 10))
my_y_ticks = np.arange(0, 10, 2)
plt.yticks(my_y_ticks)
plt.rcParams['font.sans-serif']=['SimHei']  # 中文
plt.show()




# 散点图
x = np.arange(0., 5., 0.2)
plt.xlabel('月份')
plt.ylabel('境内外确诊人数/万')
plt.rcParams['font.sans-serif']=['SimHei']  # 中文
plt.plot(x, x, 'r--', x, x ** 2, 'bs', x, x ** 3, 'g^')
plt.show()


# 饼图
data = {
    '中国': (50, '#7199cf'),
    '美国': (175, '#4fc4aa'),
    '日本': (120, '#ffff10'),
}
# 设置绘图对象的大小
fig = plt.figure(figsize=(8, 8))
cities = data.keys()
values = [x[0] for x in data.values()]
colors = [x[1] for x in data.values()]
ax1 = fig.add_subplot(111)
ax1.set_title('春季确诊病例/万')
labels = ['{}:{}'.format(city, value) for city, value in zip(cities, values)]

explode = [0.05, 0, 0] # 设置饼图的凸出显示
ax1.pie(values, labels=labels, colors=colors, explode=explode, shadow=True) # 画饼状图， 并且指定标签和对应的颜色  指定阴影效果

# plt.savefig('pie.jpg') # 保存成图片
plt.rcParams['font.sans-serif']=['SimHei']  # 中文
plt.show()


# 云词图

fp = "die.txt"
text = open("die.txt", encoding='utf-8').read()

# Generate a word cloud image
wordcloud = WordCloud(
font_path = "C:\Windows\Fonts\STXINWEI.TTF", 
width = 600, #width of the canvas.
height = 400, #height of the canvas.
max_font_size = 60,
font_step = 1,
background_color = "white",
random_state = 1,
margin = 2,
colormap = "tab20" #matplotlib colormap
).generate(text)

# Display the generated image in matplotlib way:

plt.imshow(wordcloud, interpolation='bilinear')
plt.axis("off")
plt.show()




#玫瑰

def pie_roseType():
    #链式写法
    c = (
        Pie()
        .add(
            '',
            
            [ list(z) for z in zip(['202{}年/12月/{}日'.format(y, z) for y in range(2)
                                for z in range(1,5)],[4,5,6,4])],
            radius = ['0%', '75%'],
            rosetype = 'radius',
            label_opts= opts.LabelOpts(is_show=True),
        )
        .set_global_opts(title_opts=opts.TitleOpts(title='山东每日确诊情况'))

    )
    return c


pie_roseType().render('玫瑰图.html')


# 雷达

filename = "aaa.csv"
data_x = []
data_y = []
with open(filename, encoding='utf-8') as f:
   reader = csv.reader(f)
   for data_row in reader:
      data_x.append(data_row)
x = data_x[0]
y1 = data_x[1]
y1 = [str(i) for i in y1]
y2 = [str(i) for i in data_x[2]]
y3 = [str(i) for i in data_x[3]]
def polar_datazoom_slider() -> Polar:
    c = (
        Polar(init_opts=opts.InitOpts(width="600px", height="500px"))
        .add_schema(radiusaxis_opts=opts.RadiusAxisOpts(data=x),
                    angleaxis_opts=opts.AngleAxisOpts(is_clockwise=True))
        .add("死亡", y1, type_="bar", stack="stack0")
        .add("确诊", y2, type_="bar", stack="stack0")
        .add("治愈", y3, type_="bar", stack="stack0")
        .set_global_opts(title_opts=opts.TitleOpts(title="太原疫情详情"))
)
    return c
def polar_datazoom() -> Polar:
    c2 = (
        Polar(init_opts=opts.InitOpts(width="600px", height="500px"))
        .add_schema(angleaxis_opts=opts.AngleAxisOpts(data=x, is_clockwise=True))
        .add("死亡", y1, type_="bar", stack="stack0")
        .add("确诊", y2, type_="bar", stack="stack0")
        .add("治愈", y3, type_="bar", stack="stack0")
        .set_global_opts(title_opts=opts.TitleOpts(title="太原疫情详情"))
)
    return c2
def page_draggable_layout():
    page = Page(layout=Page.DraggablePageLayout)
    page.add(
        polar_datazoom_slider(),
        polar_datazoom()
    )
    page.render("太原疫情详情.html")


page_draggable_layout()